import * as client from './client'
import urlConfig from './urlConfig'

import config from '../../config'
import * as rsaKey from './rsaKey'
import * as crypto from 'crypto'
import * as constants from 'constants'
/**
 * 请求参数
 */
export class loginRst {
    public loginInfo: string;
    public deviceIdentity: string;
    public clientSource: number;
}
/**
 * 返回数据
 */
export class loginRpn {
    public uid: string;
    public id: number
    public code: string;
    public userName: string;
    public email: string;
    public mobile: string;
    public accountUid: string;
    public userType: string;
    public userTypes: number[];
    public tempID: string;
    public token: string;
    public currencySymbol:string;
}
/**
 * 获取数据
 * @param rst 参数
 */
export async function exec(rst: loginRst) {
    /*
    if(config.openApi.enableRSA){
        let tmp = (await rsaKey.exec()).resData.publicKey;
        tmp = tmp.replace('-----BEGIN PUBLIC KEY-----','');
        tmp = tmp.replace('-----END PUBLIC KEY-----','');
        let begin = '-----BEGIN PUBLIC KEY-----\n';
        let end = '-----END PUBLIC KEY-----';
        let ret  = '';
        for(var i = 0; i < tmp.length; i += 64) {
          var a = tmp.slice(i, i + 64);
          ret  += (a + '\n');
        }
        let publicKey = begin + ret  + end;
        let keyParam = {"key": publicKey, padding: constants.RSA_PKCS1_PADDING};
        let plain = new Buffer(rst.loginInfo);
        rst.loginInfo = crypto.publicEncrypt(keyParam, plain).toString('base64');
    }
    */
    const data = await client.exec(urlConfig.login, rst);
    return JSON.parse(data) as client.RSM<loginRpn>;
}

